package com.styytech.dadabao.test.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.styytech.dadabao.widge.dialog.CustomLoadingDialog;

public class LoadingDialogMainActivity extends Activity {
	private Context mContext;
	private CustomLoadingDialog mdlg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 界面初始化控件
		LinearLayout llyt_view = new LinearLayout(this);
		Button btn_show = new Button(this);
		Button btn_close = new Button(this);
		llyt_view.addView(btn_show);
//		llyt_view.addView(btn_close);
		btn_show.setText("显示Loading框");
		btn_close.setText("关闭Loading框");
		// 设置界面
		setContentView(llyt_view);
		mContext = this;
		// 显示对话框
		btn_show.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				mdlg = getCustomLoadingDialog(mContext, mdlg, "标题", true);
				mdlg.show();
			}
		});
		// 关闭对话框
		btn_close.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View view) {
				closeDialog(mdlg);
			}
		});
	}

	/**
	 * 获得CustomLoadingDialog对象
	 * 
	 * @param context
	 * @param mdlg
	 *            返回的CustomLoadingDialog对象
	 * @param title
	 *            对话框标题，message 默认“正在执行...”，要改可重新mdlg.setMessage();
	 * @param flag
	 *            设置是否可关闭
	 * @return
	 */
	public static CustomLoadingDialog getCustomLoadingDialog(Context context,
			CustomLoadingDialog mdlg, String title, boolean flag) {
		if (mdlg != null && mdlg.dlg.isShowing()) {
			mdlg.title = title;
		} else {
			mdlg = new CustomLoadingDialog(context, title, "正在执行...", flag);
		}
		return mdlg;
	}

	public static void closeDialog(Object mdlg) {
		if (mdlg == null)
			return;
		if (mdlg instanceof CustomLoadingDialog) {
			CustomLoadingDialog md = (CustomLoadingDialog) mdlg;
			if (md.dlg.isShowing()) {
				// md.dlg.dismiss();
				md.dlg.cancel();
			}

		}
	}
}
